import json
import requests


class FbspySpider(object):
    def __init__(self):
        self.url_temp = 'http://www.fbspy.co/api/forward/adsearch'
        self.headers = {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
                        "Referer": "http://www.fbspy.co/app",
                        "Host": "www.fbspy.co",
                        "Content-Type": "application/json"}
        self.payload = {"search_result":"ads","all":0,"sort":{"field":"first_view_date","order":1},"where":[],"limit":[0,10],"is_why_all":1,"topN":10,"is_stat":0,"keys":[],"action":"search"}

    def parse_url(self, url):
        response = requests.post(url, headers=self.headers, data=json.dumps(self.payload))
        #self.payload = json.dumps(self.payload)
        return response.content.decode()

    def get_content_list(self, json_str):
        dict_ret = json.loads(json_str)
        content_list = dict_ret["ads_info"]
        return content_list

    def save_content_list(self,content_list):
        with open('fbspy.txt', 'a', encoding='utf-8') as f:
            for content in content_list:
                f.write(json.dumps(content, ensure_ascii=False))
                f.write('\n')

    def run(self):
        # 1.start_url
        # 2.发送请求 获取响应
        json_str = self.parse_url(self.url_temp)

        # 3.提取数据
        content_list = self.get_content_list(json_str)

        # 4.保存
        self.save_content_list(content_list)

        # 5.构造下一页url  进入循环


if __name__ == '__main__':
    fbspy = FbspySpider()
    fbspy.run()